/**
 * @file        relay.c
 * @author      MarkVimy
 * @Version     V1.0
 * @date        15-November-2019
 * @brief       This file includes the Motor's functions 
 * 				(based on ST Peripherals Libaray F10x V3.5.0)
 *
 */
 
/* Includes ------------------------------------------------------------------*/
#include "relay.h"

#include "delay.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* ## Global variables ## ----------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void RELAY_GPIO_init(void);

/* Private functions ---------------------------------------------------------*/
static void RELAY_GPIO_init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd(RELAY1_CLOCK|RELAY2_CLOCK|RELAY3_CLOCK|RELAY4_CLOCK, ENABLE);
    
	GPIO_InitStructure.GPIO_Pin = RELAY1_GPIO_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(RELAY1_GPIO, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = RELAY2_GPIO_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(RELAY2_GPIO, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = RELAY3_GPIO_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(RELAY3_GPIO, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = RELAY4_GPIO_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(RELAY4_GPIO, &GPIO_InitStructure);	

	RELAY1 = 0;
	RELAY2 = 0;
	RELAY3 = 0;
	RELAY4 = 0;
}

/* API functions -------------------------------------------------------------*/
void RELAY_init(void)
{
	RELAY_GPIO_init();
}

void RELAY_openLock(Lock_Names_t lock_x)
{
	switch(lock_x)
	{
		case LOCK1:
			RELAY1 = 1;
			delay_ms(500);
			RELAY1 = 0;
			break;
		case LOCK2:
			RELAY2 = 1;
			delay_ms(500);
			RELAY2 = 0;
			break;
		case LOCK3:
			RELAY3 = 1;
			delay_ms(500);
			RELAY3 = 0;
			break;
		case LOCK4:
			RELAY4 = 1;
			delay_ms(500);
			RELAY4 = 0;
			break;
		default:
			break;
	}
}
